package org.bichko.cydot;

import cytoscape.data.readers.*;
import cytoscape.logger.*;
import cytoscape.task.*;
import giny.model.*;
import java.io.*;
import java.util.*;

public class DotReader extends AbstractGraphReader {

    private static final CyLogger log = CyLogger.getLogger(DotReader.class);

    private final DotParser parser;

    public DotReader(String fileName) {
        this(fileName, null);
    }

    public DotReader(String fileName, TaskMonitor monitor) {
        super(fileName);
        try {
            parser = new DotParser(new InputStreamReader(new FileInputStream(
                    fileName), "utf-8"));
        } catch (IOException ioe) {
            throw new RuntimeException(ioe);
        }
    }

    public void setTaskMonitor(TaskMonitor monitor) {
    }

    public void read() throws IOException {
        try {
            parser.parse();
        } catch (DotIoException die) {
            throw die.getCause();
        }
    }

    public String getNetworkName() {
        return "DOT Test";
    }

    public int[] getNodeIndicesArray() {
        return graphIndices(parser.getNodes().values());
    }

    public int[] getEdgeIndicesArray() {
        return graphIndices(parser.getEdges().values());
    }

    private int[] graphIndices(Collection<? extends GraphObject> objs) {
        int[] idcs = new int[objs.size()];
        int idx = 0;
        for (GraphObject obj : objs)
            idcs[idx++] = obj.getRootGraphIndex();
        return idcs;
    }

}
